
package com.probiz.estoresa.inventory.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.inventory.service.InventoryManager;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.BrandManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.ProductSkuManager;
import com.probiz.estore.product.service.ProductTypeManager;
import com.probiz.estore.webapp.util.RequestUtil;

public class InventoryController extends GenericStoreAdminController<ProductSku> {
	private InventoryManager	inventoryManager	= null;
	private ProductManager productManager = null;
	private ProductCategoryManager productCategoryManager = null;
    private ProductSkuManager productSkuManager = null;
    private BrandManager brandManager = null;
    private ProductTypeManager productTypeManager = null;

	public void setInventoryManager(InventoryManager inMgr) {
		this.inventoryManager = inMgr;
	}
	public void setProductSkuManager(ProductSkuManager productSkuManager) {
		this.productSkuManager = productSkuManager;
	}
	
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}
	
	public void setBrandManager(BrandManager brandManager) {
		this.brandManager = brandManager;
	}
	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}
	@Override
	protected String getEntityName(ProductSku entity) {
		return entity.getProductSkuName();
	}


	protected void initController() throws Exception {
		mgr = productSkuManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, ProductSku sku,
			BindException errors) {
	}


	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("inventory/inventoryList");
		ProductCategory treeRoot = productCategoryManager.getById(1);
		Set<ProductCategory> categorySet = treeRoot.getChildren();
		List<ProductCategory> categoryList = new ArrayList<ProductCategory>();
		categoryList.addAll(categorySet);
		mv.addObject("categoryList", categoryList);
		mv.addObject("treeRoot", treeRoot);
		mv.addObject("productTypeList",productTypeManager.listAll());
		mv.addObject("brandList", brandManager.listAll()); 
		return mv;
	}
	
	public ModelAndView search(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("inventory/include/inventoryListBody");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria();
		try {
			// 绑定参数
			bind(request, productSearchCriteria);
			productSearchCriteria.setIsInventorySearch(Constants.FLAG_TRUE); //是库存方面的搜索
			List<Product> productList = productManager.searchByCriteriaForController(productSearchCriteria,	searchCriteria);
			mv.addObject("productList", productList);
			mv.addObject("productSearchCriteria", productSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	
	/**
	 * 更新可售库存
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView updateOnhand(HttpServletRequest request,HttpServletResponse response){
		ProductSku productSku = formBackingObject(request);
		inventoryManager.doAdjustQuantityOnHand(productSku, RequestUtil.getInteger(request, "onhand"),null);
		ModelAndView mv = new ModelAndView("inventory/include/inventoryInfo");
		mv.addObject("sku", productSku);
		return mv;
	}
	
	/**
	 * 更新低库存警告数量
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView updateReorderMinimnm(HttpServletRequest request,HttpServletResponse response){
		ProductSku productSku = formBackingObject(request);
		inventoryManager.doAdjustReorderMinimnm(productSku, RequestUtil.getInteger(request, "reorderMinimnm"),null);
		ModelAndView mv = new ModelAndView("inventory/include/inventoryInfo");
		mv.addObject("sku", productSku);
		return mv;
	}
	

	
}
